/* Copyright (c) [2014 Baidu]. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * File Name          : 
 * Author             : 
 * Version            : $Revision:$
 * Date               : $Date:$
 * Description        : 
 *                      
 * HISTORY:
 * Date               | Modification                    | Author
 * 28/03/2014         | Initial Revision                | 
 
 */

#import "ios_lib.h"
#import "protocol.h"
#import "L2_Phone.h"
#import "BlueToothLib.h"
/****************************************************************************
* Tempory send buffer
*****************************************************************************/
uint8_t global_reponse_buffer[GLOBAL_RESPONSE_BUFFER_SIZE];

void* g_pL2_command_key_func_context;
L2_command_key_resolove g_pL2_command_key_func;

void send_L2_command_with_single_key(uint8_t command, uint8_t key, uint8_t* p_value, uint16_t value_length, BLECallBack callback)
{
	L2_Send_Content sendContent;

	global_reponse_buffer[0] = command;    /*command id*/
	global_reponse_buffer[1] = L2_HEADER_VERSION;   /*L2 header version */

	global_reponse_buffer[2] = key;         /*first key, */

	global_reponse_buffer[3] = (value_length >> 0) & 0xFF;
	global_reponse_buffer[4] = (value_length >> 8) & 0x01;

	if (p_value && (value_length > 0))
	{
		memcpy(&global_reponse_buffer[5], p_value, value_length);
	}

	sendContent.callback  = NULL;
	sendContent.content  = global_reponse_buffer;
	sendContent.length   = L2_HEADER_SIZE + L2_PAYLOAD_HEADER_SIZE + value_length; /*length of whole L2*/

	L1_send(&sendContent, callback);
}

void set_local_time(uint32_t second, BLECallBack callback)
{
	send_L2_command_with_single_key(SET_CONFIG_COMMAND_ID, KEY_TIME_SETTINGS, (uint8_t*)&second, 4, callback);
}

void alarm_setting(alarm_setting_t * alarm, uint8_t alarm_count, BLECallBack callback)
{
	send_L2_command_with_single_key(SET_CONFIG_COMMAND_ID, KEY_ALARM_SETTINGS, (uint8_t*)alarm, alarm_count * sizeof(alarm_setting_t),callback);
}

void alarm_get(BLECallBack callback)
{
	send_L2_command_with_single_key(SET_CONFIG_COMMAND_ID, KEY_ALARM_LIST_REQUEST, NULL, 0,callback);
}

void get_device_battery(BLECallBack callback)
{
    send_L2_command_with_single_key(BLUETOOTH_DEVICE_COMMAND_ID, DEVICE_INFO_KEY_GET_BATTERY, NULL, 0,callback);
}

void userinfo_setting(user_info_t * p_info, BLECallBack callback)
{
	send_L2_command_with_single_key(SET_CONFIG_COMMAND_ID, KEY_USER_INFO_SETTING, (uint8_t*)p_info, sizeof(user_info_t), callback);
}

void steptarget_setting(uint32_t uSteps, BLECallBack callback)
{
	send_L2_command_with_single_key(SET_CONFIG_COMMAND_ID, KEY_STEP_TARGET_SETTINGS, (uint8_t*)&uSteps, 4, callback);
}

void request_sport_data(uint8_t request, BLECallBack callback)
{
	send_L2_command_with_single_key(HEALTH_DATA_COMMAND_ID, KEY_REQUEST_SPORT_DATA, &request, 1, callback);
}

void ota_control(uint8_t cmd, BLECallBack callback)
{
	send_L2_command_with_single_key(FIRMWARE_UPDATE_CMD_ID, KEY_OTA_UPDATE_REQUEST, &cmd, 1, callback);
}

void ota_control_2(uint8_t cmd, uint32_t version, BLECallBack callback)
{
	uint8_t cmdbuffer[8];
	cmdbuffer[0] = cmd;
	cmdbuffer[1] = (version >>  0) & 0xFF;
	cmdbuffer[2] = (version >>  8) & 0xFF;
	cmdbuffer[3] = (version >> 16) & 0xFF;
	cmdbuffer[4] = (version >> 24) & 0xFF;

	send_L2_command_with_single_key(FIRMWARE_UPDATE_CMD_ID, KEY_OTA_UPDATE_REQUEST, cmdbuffer, 5, callback);
}

uint8_t ota_send_buffer[256] = {0};
void ota_send(uint8_t * pBuffer, uint16_t len, BLECallBack callback)
{
	if (len && pBuffer)
	{
		memset(ota_send_buffer, 0, 256);
		memcpy(&ota_send_buffer[3], pBuffer, len);
		send_L2_command_with_single_key(FIRMWARE_UPDATE_CMD_ID, KEY_OTA_DATA_SEND, ota_send_buffer, (len+3), callback);
	}
}

void ota_send_over(uint16_t crc, uint32_t file_len, BLECallBack callback)
{
	uint8_t cmdbuffer[8];
	cmdbuffer[0] = (file_len >>  0) & 0xFF;;
	cmdbuffer[1] = (file_len >>  8) & 0xFF;;
	cmdbuffer[2] = (file_len >> 16) & 0xFF;
	cmdbuffer[3] = (file_len >> 24) & 0xFF;
	cmdbuffer[4] = (crc >>  0) & 0xFF;
	cmdbuffer[5] = (crc >>  8) & 0xFF;
	send_L2_command_with_single_key(FIRMWARE_UPDATE_CMD_ID, KEY_OTA_DATA_SENDOVER, cmdbuffer, 6, callback);
}

void send_notification(uint8_t command, uint8_t evt_flag, uint8_t category_id, uint8_t category_count, uint32_t notificationUID, BLECallBack callback)
{
	uint8_t notifyBuffer[12] = {0};
	//EventFlags	CategoryID	CategoryCount	NotificationUID
	//	1				1			1				4
	notifyBuffer[0] = evt_flag;
	notifyBuffer[1] = category_id;
	notifyBuffer[2] = category_count;
	notifyBuffer[3] = (notificationUID >> 0) & 0xFF;
	notifyBuffer[4] = (notificationUID >> 8) & 0xFF;
	notifyBuffer[5] = (notificationUID >> 16) & 0xFF;
	notifyBuffer[6] = (notificationUID >> 24) & 0xFF;

	send_L2_command_with_single_key(NOTIFY_COMMAND_ID, command, notifyBuffer, 8, callback);
}

/***********************************************************************
* para introduction
* data                                   :      just the full of L2
* content_length :      length of data
* resolve_state  :  L1 receive data length
************************************************************************/
uint32_t L2_frame_resolve_from_band(uint8_t * data, uint16_t length,RECEIVE_STATE * resolve_state)
{
    //para check
    if((!data) || (length == 0)) {
        return NRF_ERROR_INVALID_PARAM;
    }

    BLUETOOTH_COMMUNICATE_COMMAND command_id;
    uint8_t version_num;                                            /* L2 version number */
    uint8_t first_key;                                                      /* first key of L2 payload*/
    uint16_t first_value_length;            /* length of first value */

    command_id  = (BLUETOOTH_COMMUNICATE_COMMAND)data[0];
    version_num = data[1];
    version_num = version_num;                      /*current not use it*/

	first_key = data[2];
	first_value_length = (((data[4]<< 8) | data[3]) & 0x1FF);

//	if (g_pL2_command_key_func)
//	{
//		g_pL2_command_key_func(command_id, first_key, data + L2_FIRST_VALUE_POS, first_value_length, g_pL2_command_key_func_context);
//	}

    [BlueToothLib returnDataForCommand:command_id
                                   key:first_key
                                 value:data + L2_FIRST_VALUE_POS
                                length:first_value_length];

    /*resolve complete and restart receive*/
    *resolve_state = WAIT_START;
    return NRF_SUCCESS;
}
